import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 */

//动态顺序表的各接口实现
class SeqList{
    public int[] elem;
    public int usedSize;

    //构造函数
    public SeqList(){
        this.elem = new int[10];
    }

    //1、打印顺序表
    public void display(){
        for (int i = 0; i < this.usedSize; i++) {
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }

    //2、获取顺序表长度
    public int size(){
        return this.usedSize;
    }

    //3、在pos位置新增元素
    public void add(int pos,int data){
        if(pos < 0 || pos > this.usedSize){
            System.out.println("pos的位置不合法！");
            return;
        }
        if(isFull(pos)){
            this.elem = Arrays.copyOf(this.elem,this.usedSize * 2);
        }
        for (int i = this.usedSize - 1; i >= pos; i--) {
            this.elem[i+1] = this.elem[i];
        }
        elem[pos] = data;
        usedSize++;
    }

    public boolean isFull(int pos){
        return pos > this.elem.length;
    }

    //4、判断是否包含某个元素
    public boolean contain(int toFind){
        for (int i = 0; i < this.usedSize; i++) {
            if(toFind == this.elem[i]){
                return true;
            }
        }
        return false;
    }

    //5、查找某个元素对应的位置
    public int search(int toFind){
        for (int i = 0; i < this.usedSize; i++) {
            if(toFind == this.elem[i]){
                return i;
            }
        }
        return -1;
    }

    //6、获取pos位置的元素
    public int getPos(int pos){
        if(pos < 0 || pos >= this.usedSize){
            System.out.println("pos位置不合法！");
            return -1;
        }
        if(isEmpty()){
            System.out.println("顺序表为空！");
            return -1;
        }
            return elem[pos];
    }

    public boolean isEmpty(){
        return this.usedSize == 0;
    }

    //给pos位置元素设为value
    public void setPos(int pos,int value){
        if(pos < 0 || pos >= this.usedSize){
            System.out.println("pos位置不合法！");
        }
        if(isEmpty()){
            System.out.println("顺序表为空！");
        }
        this.elem[pos] = value;
        display();
    }

    //删除关键字Remove
    public void remove(int toRemove){
        int key = search(toRemove);
        if(key == -1){
            System.out.println("没有要找的元素！");
            return;
        }
        for (int i = key; i < this.usedSize - 1; i++) {
            elem[i] = elem[i+1];
        }
        usedSize--;
    }

    public void clear(){
        this.usedSize = 0;
    }
}

public class Main {
    public static void main(String[] args) {
        SeqList seqList = new SeqList();
        seqList.add(0,1);
        seqList.add(1,2);
        seqList.add(2,3);
        seqList.add(3,4);
        seqList.display();
        System.out.println(seqList.contain(3));
        seqList.display();
        System.out.println("=========");
        System.out.println(seqList.search(9));
        System.out.println("==========");
        System.out.println(seqList.getPos(2));
        System.out.println("===========");
        seqList.setPos(5,8);
        System.out.println("==========");
        seqList.remove(2);
        seqList.display();
        System.out.println("==========");
        seqList.clear();
        seqList.display();
    }
}
